#region Imported Namespaces
using System;
using System.Collections.Specialized;
using System.Data;
using System.Web.UI.WebControls;

using PHSRAG.Insight21.Context;
using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.SearchContext
{
	#region Class TransactionSearchContext
	/// <summary>
	/// The TransactionSearchContext class encapsulates the search context associated with the TransactionSearch control.
	/// </summary>
	public class TransactionSearchContext : SearchContext
	{
		#region Constants
		private static string TransactionSearchContextKey = GenerateKey.MakeKey("transactionSearchContext_43B08565-B16E-4f01-869E-953B5D3371F7",GenerateKey.Agreement);
		#endregion

		#region Private Types
		/// <summary>
		/// The DataTableIndex enumeration defines the index of each table (which holds data pertaining to the set of
		/// drop-down-lists) within the data container.
		/// </summary>
		private enum DataTableIndex
		{
			AgreementStatus, AgreementType, ActivityType, Location, SponsorType, FundStatus, Category, POAPOrderTypes, ALLTransactionTypes, TransactionTypeValues, TransactionDateTypeValues, Max
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Create an instance of the TransactionSearchContext class. Since this object does not hold any state (other than
		/// whatever the base class holds), there is nothing to initialize.
		/// </summary>
		/// <param name="cachePolicy">Cache policy to use</param>
		public TransactionSearchContext(ICachePolicy cachePolicy) : base(cachePolicy)
		{
		}
		#endregion

		#region Public Properties
		/// <summary>
		/// Get the data table from the data container which contains data for the activity-type drop-down-list.
		/// </summary>
		public DataTable ActivityTypeData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.ActivityType]; }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the agreement-type drop-down-list.
		/// </summary>
		public DataTable AgreementTypeData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.AgreementType]; }
		}

		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return TransactionSearchContextKey; } }

		/// <summary>
		/// Get the data table from the data container which contains data for the location drop-down-list.
		/// </summary>
		public DataTable LocationData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.Location]; }
		}
		
		/// <summary>
		/// Get the data table from the data container which contains data for the sponsor-type drop-down-list.
		/// </summary>
		public DataTable SponsorTypeData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.SponsorType]; }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the agreement status drop-down-list.
		/// </summary>
		public DataTable AgreementStatusData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.AgreementStatus]; }
		}
		/// <summary>
		/// Get the data table from the data container which contains data for the fund status drop-down-list.
		/// </summary>
		public DataTable FundStatusData
		{
			get { LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.FundStatus]; }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the Category drop-down-list.
		/// </summary>
		public DataTable Category
		{
			get {  LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.Category]; }
		}

		/// <summary>
		/// Get the POAP order types data table from the data container which contains data for the Type drop-down-list.
		/// this will be used for Purchasing search pages
		/// </summary>
		public DataTable POAPOrderTypes
		{
			get {  LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.POAPOrderTypes]; }
		}

		/// <summary>
		/// Get the ALL Transaction Types data table from the data container which contains data for the Type drop-down-list.
		/// this will be used for Transaction Overview to load ordertype dropdown in search control
		/// </summary>
		public DataTable ALLTransactionTypes
		{
			get {  LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.ALLTransactionTypes]; }
		}

		/// <summary>
		/// Get the data table from the data container which contains data for the Transaction Type Values drop-down-list.
		/// </summary>
		public DataTable TransactionTypeValues
		{
			get {  LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.TransactionTypeValues]; }
		}
		
		/// <summary>
		/// Get the data table from the data container which contains data for the Transaction Date Type Values drop-down-list.
		/// </summary>
		public DataTable TransactionDateTypeValues
		{
			get {  LoadTransactionSearchData(); return DataContainer.Tables[(int)DataTableIndex.TransactionDateTypeValues]; }
		}
		#endregion

		#region BusinessObject Methods
		/// <summary>
		/// Cache this instance of the TransactionSearchContext object using the current cache policy.
		/// </summary>
		public override void Cache()
		{
			CachePolicy.Cache(CacheKey, this);
		}

		/// <summary>
		/// Load agreement search data into the data container. The loading is done by invoking a stored procedure.
		/// </summary>
		/// <param name="connectionString">Connection string to the database</param>
		/// <param name="args">Arugments to the invoked stored procedure</param>
		public override void Load(string connectionString, params object[] args)
		{
			LoadTransactionSearchData(connectionString);
		}

		/// <summary>
		/// Remove this instance of the TransactionSearchContext object from the cache using the current cache policy.
		/// </summary>
		public override void UnCache()
		{
			CachePolicy.UnCache(CacheKey);
		}
		#endregion

		#region SearchContext Members
		/// <summary>
		/// Reset the internal search state.
		/// </summary>
		public override void Reset(string pageName)
		{
			DataContainer.Reset();
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Invoke the helper method of the same name to load the search data using the connection string 
		/// configured in the application configuration file.
		/// </summary>
		private void LoadTransactionSearchData()
		{
			LoadTransactionSearchData(DataAccess.ConnectionString);
		}

		/// <summary>
		/// Load the data container with the agreement search data. Each "section" of the agreement search is 
		/// loaded from a seperate table.
		/// Note that this method (via the overload LoadTransactionSearchData() WILL be invoked by every property
		/// to ensure that the tables are indeed loaded with the relevant search data. So as to avoid superfluous
		/// loads, a check is made on the data container reference and on the table count within that data container.
		/// </summary>
		/// <param name="connectionString">Database connection string to use for the load</param>
		private void LoadTransactionSearchData(string connectionString)
		{
			if ((DataContainer == null) || (DataContainer.Tables.Count == 0))
			{
				UserContext userContext = CachePolicy.GetCachedObject(UserContext.CacheKey) as UserContext;
				System.Diagnostics.Trace.Assert(userContext != null);

				DataContainer = DataAccess.GetDataSet(connectionString, "GetTransactionSearchData",
					DataAccess.BuildSqlParameterArray("@domainUserID", SqlDbType.Int, userContext.DomainUserId));
			}
		}
		#endregion
	}
	#endregion
}
// --- EOF ---
